package com.way.fileexplorer.activities;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.way.fileexplorer.R;
import com.way.fileexplorer.adapters.FileListAdapter;
import com.way.fileexplorer.fragments.DetailedListFragment;
import com.way.fileexplorer.util.FileUtil;
import com.way.fileexplorer.util.StorageUtil;

public class FilePasteActivity extends Activity {
	private File mTopPath;
	private FileListAdapter mAdapter;
	private boolean bIsSdCard = false;
	private ListView mListView = null;
	private View mEmptyView = null;
	private TextView mPathBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_file_paste);
		mListView = (ListView) this.findViewById(R.id.list_paste);
		mEmptyView = (View) this.findViewById(R.id.emptyView);
		mPathBar = (TextView) findViewById(R.id.path_bar_file_explore_activity);

		Intent data = getIntent();
		if (null != data) {
			setResult(DetailedListFragment.RESULT_RESELECT, null);
			bIsSdCard = data.getBooleanExtra(StorageUtil.SDCARD_TYPE, false);
			mTopPath = bIsSdCard ? StorageUtil.getExternalStorage()
					: StorageUtil.getInternalStorage();
			mAdapter = FileListAdapter.getFileListAdapter(this,mListView).initWithPath(
					mTopPath);
			mPathBar.setText(mAdapter.getCurrentPath().toString());
			mListView.setAdapter(mAdapter);
			mListView.setEmptyView(mEmptyView);
            // Bug 201542 Begin
            mListView.setOnScrollListener(new FileListScrollListener(mAdapter));
            // Bug 201542 End
			mListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					mAdapter.execute(arg2);
					mPathBar.setText(mAdapter.getCurrentPath().toString());
				}

			});
		} else {
			finish();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.paste_activity_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_paste_mode_paste:
			// TODO: use paste mode
            Intent intent = new Intent();
            intent.putExtra("paste_path", mAdapter.getCurrentPath()
                    .getPath());
            intent.putExtra("isSdStor", bIsSdCard);
            setResult(RESULT_OK, intent);
            finish();
			break;
		case R.id.action_paste_mode_cancel:
			FileUtil.clearPaste();
			setResult(RESULT_CANCELED, null);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		if (mAdapter.popupFolder()) {
			super.onBackPressed();
		}
		mPathBar.setText(mAdapter.getCurrentPath().toString());
	}
}
